/**
 * 详情对话框
 */
var StockStrategyInfoDlg = {
    data: {
        code: "",
        name: "",
        buyPrice: "",
        stopLossPrice: "",
        takeProfitPrice: "",
        buyDate: "",
        profitRate: "",
        sellDate: "",
        buyAmount: "",
        sellAmount: "",
        status: "",
        eventType: "",
        gmtCreate: "",
        gmtModified: ""
    }
};

layui.use(['form', 'laypage', 'admin', 'ax'], function () {
        var $ = layui.jquery;
        var $ax = layui.ax;
        var form = layui.form;
        var admin = layui.admin;
        laypage = layui.laypage;
        var laydate = layui.laydate;
        //让当前iframe弹层高度适应
        admin.iframeAuto();
        //获取详情信息，填充表单
        var StockStrategy = {
            datas: [],
            count: 0
        };
        StockStrategy.load_data = function (data) {
            var ajax = new $ax(Feng.ctxPath + "/stock/stockStrategy/getPosition");
            var result = ajax.start();
            console.log(JSON.stringify(result))
            var datas = result.data
            StockStrategy.count = result.count
            StockStrategy.datas = datas
            $('#stockStrategyForm').html("")
            for (stock_data in datas) {
                if (datas[stock_data].eventType == 1) {
                    datas[stock_data].eventType = '买入'
                }
                if (datas[stock_data].eventType == 2) {
                    datas[stock_data].eventType = '卖出'
                }
                if (datas[stock_data].eventType == 3) {
                    datas[stock_data].eventType = '交易完成'
                }

                var content = ' <div class="caller-main caller-fl">\n' +
                    '                        <p>\n' +
                    '                            <i class="layui-icon layui-icon-template-1"></i>\n' +
                    '                            <strong>' + datas[stock_data].证券名称 + '(' + datas[stock_data].证券代码 + ')</strong>\n' +
                    '                            <em>盈亏：' + datas[stock_data].盈亏 + '</em></p>\n' +
                    '                        <p class="caller-adds">\n' +
                    '                            <i class="layui-icon layui-icon-location"></i>\n' +
                    '                            <strong style="font-size: 14px">股票余额：</strong>\n' +
                    '                            <i style="font-size: 14px;color: red"> ' + datas[stock_data].股票余额 + '</i>\n' +
                    '                           \n' +
                    '                            <i class="layui-icon layui-icon-location"></i>\n' +
                    '                            <strong style="font-size: 14px">成本价：</strong>\n' +
                    '                            <i style="font-size: 14px;color: red"> ' + datas[stock_data].成本价 +
                    ' </i>\n' +
                    '                        </p>\n' +
                    '                        <p class="caller-adds">\n' +
                    '                            <i class="layui-icon layui-icon-location"></i>\n' +
                    '                            <strong style="font-size: 14px">市值：</strong>\n' +
                    '                            <i style="font-size: 14px;color: red"> ' + datas[stock_data].市值 +
                    ' </i>\n' +
                    '                           \n' +
                    '                            <i class="layui-icon layui-icon-location"></i>\n' +
                    '                            <strong style="font-size: 14px">可用余额：</strong>\n' +
                    '                            <i style="font-size: 14px;color: red"> ' + datas[stock_data].可用余额 +
                    ' </i>\n' +
                    '</p>\n' +
                    '                        <p class="caller-adds">\n' +
                    '                            <i class="layui-icon layui-icon-location"></i>\n' +
                    '                            <strong style="font-size: 14px">冻结数量：' + ' </strong>\n' +
                    '<i style="font-size: 14px;color: red"> ' + datas[stock_data].冻结数量 +
                    ' </i>\n' +
                    '</p>\n' +
                    '                        <p class="caller-adds">\n' +
                    '                            <i class="layui-icon layui-icon-location"></i>\n' +
                    '                            <strong style="font-size: 14px">市价：' + ' </strong>\n' +
                    '<i style="font-size: 14px;color: red"> ' + datas[stock_data].市价 +
                    ' </i>\n' +
                    '</p>\n' +
                    '</div>'

                $('#stockStrategyForm').append(content);
                $("button.layui-btn").click(StockStrategy.listenButtonEvent)

            }
        }
        StockStrategy.load_data('1')
        $("#totalProfitRatio").html(StockStrategy.totalProfitRatio)
        if (StockStrategy.datas.length > 0) {
            laypage.render({
                elem: 'demo-template-caller1'
                , count: StockStrategy.count  //数据总数
                , skin: '#AF0000'
                , jump: function (obj, first) {
                    StockStrategy.load_data(obj.curr)
                    $("button.caller-fr").click(function () {
                        console.log("edit event")
                        console.log($(this).attr("data-id"))
                        dataId = $(this).attr("data-id")
                        console.log($(this).html())
                        StockStrategy.openModifyDlg(dataId)

                    })

                }
            });
        }
        // 添加按钮点击事件
        $('#btnAdd').click(function () {
            StockStrategy.openAddDlg();
        })
        $('#btnReload').click(function () {
            StockStrategy.reloadTradeClient();
        });
        StockStrategy.reloadTradeClient = function () {
            console.log("reloadTradeClient")
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/stock/stockStrategy/reloadTradeClient", function (data) {
                    Feng.success("重载交易客户端成功!");
                    StockStrategy.load_data('1');
                }, function (data) {
                    console.log(JSON.stringify(data))
                    Feng.error("重载交易客户端失败!" + data.responseJSON.message + "!");
                });
                ajax.start();
            };
            Feng.confirm("是否重载交易客户端?", operation);
        };
        $('#btnObtain').click(function () {
            StockStrategy.getBalance();
        });
        StockStrategy.getBalance = function () {
            console.log("getBalance")
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/stock/stockStrategy/reloadTradeClient", function (data) {
                    Feng.success("重载交易客户端成功!");
                    StockStrategy.load_data('1');
                }, function (data) {
                    console.log(JSON.stringify(data))
                    Feng.error("重载交易客户端失败!" + data.responseJSON.message + "!");
                });
                ajax.start();
            };
            Feng.confirm("是否重载交易客户端?", operation);
        };


        /**
         * 弹出添加对话框
         */
        StockStrategy.openAddDlg = function () {
            window.location.href = Feng.ctxPath + '/stock/stockStrategy//add';
        };
        /**
         * 弹出修改对话框
         */
        StockStrategy.openModifyDlg = function (data) {
            console.log("open modify dialog")
            window.location.href = Feng.ctxPath + '/stock/stockStrategy//edit?id=' + data;
        };
        StockStrategy.sellDirect = function (data) {
            console.log("sell direct")
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/stock/stockStrategy/sellDirectly", function (data) {
                    Feng.success("核按钮成功!");
                    StockStrategy.load_data('1');
                }, function (data) {
                    console.log(JSON.stringify(data))
                    Feng.error("核按钮失败!" + data.responseJSON.message + "!");
                });
                ajax.set("id", data);
                ajax.start();
            };
            Feng.confirm("是否核按钮?", operation);
            //window.location.href = Feng.ctxPath + '/stock/stockStrategy//edit?id=' + data;
        };

        StockStrategy.listenButtonEvent = function () {
            ewEvent = $(this).attr("ew-event")
            dataId = $(this).attr("data-id")
            if (ewEvent != '' && ewEvent == 'modifyDialog') {
                StockStrategy.openModifyDlg(dataId)
            } else if (ewEvent != '' && ewEvent == 'delDialog') {
                StockStrategy.onDeleteItem(dataId)
            } else if (ewEvent != '' && ewEvent == 'sellDirect') {
                StockStrategy.sellDirect(dataId)
            }

        }
        StockStrategy.onDeleteItem = function (data) {
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/stock/stockStrategy/delete", function (data) {
                    Feng.success("删除成功!");
                    StockStrategy.load_data('1');
                }, function (data) {
                    Feng.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.set("id", data);
                ajax.start();
            };
            Feng.confirm("是否删除?", operation);
        };
        $("button.layui-btn").click(StockStrategy.listenButtonEvent)


    }
)
